<?php
/** 
*
* @package acp
* @version $Id: acp_topic_tags.php 82 2007-07-02 06:26:31Z highwayoflife $
* @copyright (c) 2007 Star Trek Guide Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @package module_install
*/
class acp_topic_tags_info
{
	function module()
	{
		return array(
			'filename'	=> 'acp_topic_tags',
			'title'		=> 'ACP_TOPIC_TAGS',
			'version'	=> '1.0.B1',
			'modes'		=> array(
				'global'		=> array('title' => 'ACP_GLOBAL_TOPIC_TAGS', 'auth' => 'acl_a_forum', 'cat' => array('ACP_MANAGE_FORUMS')),
			),
		);
	}

	function install($topic_tags_version)
	{
		global $db, $phpbb_root_path, $phpEx, $user;
		
		// MySQL versions prior to 4.1.3 can’t handle Charsets, so we find out what we are dealing with here
		// later versions of this MOD will have full SQL layer support
		switch ($db->sql_layer)
		{
			case 'mysql':
				$db_type = 'mysql_40';
			break;
		
			case 'mysql4':
				if (version_compare($db->mysql_version, '4.1.3', '>='))
				{
					$db_type = 'mysql_41';
				}
				else
				{
					$db_type = 'mysql_40';
				}
			break;
		
			case 'mysqli':
				$db_type = 'mysql_41';
			break;
			
			case 'mssql':
			case 'mssql_odbc':
				$db_type = 'mssql';
			break;
			
			default:
				$db_type = $db->sql_layer;
			break;
		}
		
		switch ($db_type)
		{
			case 'firebird':
				$sql = 'CREATE TABLE ' . TOPIC_TAGS_TABLE . " (
					topic_tag_id INTEGER NOT NULL,
					topic_tag_name VARCHAR(255) CHARACTER SET NONE DEFAULT '' NOT NULL,
					topic_tag_colour INTEGER DEFAULT 0 NOT NULL,
					topic_tag_desc VARCHAR(255) CHARACTER SET NONE DEFAULT '' NOT NULL,
					forum_id INTEGER DEFAULT 0 NOT NULL,
					topic_tag_global INTEGER DEFAULT 0 NOT NULL
				);;
				
				ALTER TABLE " . TOPIC_TAGS_TABLE . ' ADD PRIMARY KEY (topic_tag_id);;
				
				CREATE GENERATOR ' . TOPIC_TAGS_TABLE . '_gen;;
				SET GENERATOR ' . TOPIC_TAGS_TABLE . '_gen TO 0;;
				
				CREATE TRIGGER t_' . TOPIC_TAGS_TABLE . " FOR " . TOPIC_TAGS_TABLE . '
				BEFORE INSERT
				AS
				BEGIN
					NEW.topic_tag_id = GEN_ID(' . TOPIC_TAGS_TABLE . '_gen, 1);
				END;;';
		
				$db->sql_query($sql);
			break;
		
			case 'mssql':
				$sql = 'BEGIN TRANSACTION
				GO
				
				CREATE TABLE [' . TOPIC_TAGS_TABLE . "] (
					[topic_tag_id] [int] IDENTITY (1, 1) NOT NULL ,
					[topic_tag_name] [varchar] (255) DEFAULT ('') NOT NULL ,
					[topic_tag_colour] [int] DEFAULT (0) NOT NULL ,
					[topic_tag_desc] [varchar] (255) DEFAULT ('') NOT NULL ,
					[forum_id] [int] DEFAULT (0) NOT NULL ,
					[topic_tag_global] [int] DEFAULT (0) NOT NULL 
				) ON [PRIMARY]
				GO
				
				ALTER TABLE [" . TOPIC_TAGS_TABLE . '] WITH NOCHECK ADD 
					CONSTRAINT [PK_' . TOPIC_TAGS_TABLE . '] PRIMARY KEY  CLUSTERED 
					(
						[topic_tag_id]
					)  ON [PRIMARY] 
				GO		
				
				COMMIT
				GO';
		
				$db->sql_query($sql);
			break;
		
			case 'mysql_40':
				$sql = 'CREATE TABLE ' . TOPIC_TAGS_TABLE . " (
					topic_tag_id mediumint(8) UNSIGNED NOT NULL auto_increment,
					topic_tag_name varbinary(255) DEFAULT '' NOT NULL,
					topic_tag_colour int(11) UNSIGNED DEFAULT '0' NOT NULL,
					topic_tag_desc varbinary(255) DEFAULT '' NOT NULL,
					forum_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
					topic_tag_global tinyint(1) UNSIGNED DEFAULT '0' NOT NULL,
					PRIMARY KEY (topic_tag_id)
				);";
		
				$db->sql_query($sql);
		
				$sql = 'ALTER TABLE ' . TOPICS_TABLE . " ADD COLUMN topic_tag_id mediumint(8) UNSIGNED NOT NULL DEFAULT '0'";
				$db->sql_query($sql);
				
				$sql = 'ALTER TABLE ' . FORUMS_TABLE . " ADD COLUMN forum_topic_tags tinyint(1) UNSIGNED NOT NULL DEFAULT '0';";
				$db->sql_query($sql);
			break;
		
			case 'mysql_41':
				$sql = 'CREATE TABLE ' . TOPIC_TAGS_TABLE . " (
					topic_tag_id mediumint(8) UNSIGNED NOT NULL auto_increment,
					topic_tag_name varchar(255) DEFAULT '' NOT NULL,
					topic_tag_colour int(11) UNSIGNED DEFAULT '0' NOT NULL,
					topic_tag_desc varchar(255) DEFAULT '' NOT NULL,
					forum_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
					topic_tag_global tinyint(1) UNSIGNED DEFAULT '0' NOT NULL,
					PRIMARY KEY (topic_tag_id)
				) CHARACTER SET `utf8` COLLATE `utf8_bin`;";
		
				$db->sql_query($sql);
		
				$sql = 'ALTER TABLE ' . TOPICS_TABLE . " ADD COLUMN topic_tag_id mediumint(8) UNSIGNED NOT NULL DEFAULT '0'";
				$db->sql_query($sql);
				
				$sql = 'ALTER TABLE ' . FORUMS_TABLE . " ADD COLUMN forum_topic_tags tinyint(1) UNSIGNED NOT NULL DEFAULT '0';";
				$db->sql_query($sql);
			break;
		
			case 'oracle':
				$sql = 'CREATE TABLE ' . TOPIC_TAGS_TABLE . " (
					topic_tag_id number(8) NOT NULL,
					topic_tag_name varchar2(255) DEFAULT '' ,
					topic_tag_colour number(11) DEFAULT '0' NOT NULL,
					topic_tag_desc varchar2(255) DEFAULT '' ,
					forum_id number(8) DEFAULT '0' NOT NULL,
					topic_tag_global number(1) DEFAULT '0' NOT NULL,
					CONSTRAINT pk_" . TOPIC_TAGS_TABLE . ' PRIMARY KEY (topic_tag_id)
				)
				/
				
				CREATE SEQUENCE ' . TOPIC_TAGS_TABLE . '_seq
				/
				
				CREATE OR REPLACE TRIGGER t_' . TOPIC_TAGS_TABLE . '
				BEFORE INSERT ON ' . TOPIC_TAGS_TABLE . '
				FOR EACH ROW WHEN (
					new.topic_tag_id IS NULL OR new.topic_tag_id = 0
				)
				BEGIN
					SELECT ' . TOPIC_TAGS_TABLE . '_seq.nextval
					INTO :new.topic_tag_id
					FROM dual;
				END;
				/
				';
		
				$db->sql_query($sql);
			break;
		
			case 'postgres':
				$sql = 'BEGIN;
				
				CREATE SEQUENCE ' . TOPIC_TAGS_TABLE . '_seq;
				
				CREATE TABLE ' . TOPIC_TAGS_TABLE . " (
					topic_tag_id INT4 DEFAULT nextval('' . TOPIC_TAGS_TABLE . '_seq'),
					topic_tag_name varchar(255) DEFAULT '' NOT NULL,
					topic_tag_colour INT4 DEFAULT '0' NOT NULL CHECK (topic_tag_colour >= 0),
					topic_tag_desc varchar(255) DEFAULT '' NOT NULL,
					forum_id INT4 DEFAULT '0' NOT NULL CHECK (forum_id >= 0),
					topic_tag_global INT2 DEFAULT '0' NOT NULL CHECK (topic_tag_global >= 0),
					PRIMARY KEY (topic_tag_id)
				);
						
				COMMIT;";
		
				$db->sql_query($sql);
			break;
		
			case 'sqlite':
				$sql = 'BEGIN TRANSACTION;
				
				CREATE TABLE ' . TOPIC_TAGS_TABLE . " (
					topic_tag_id INTEGER PRIMARY KEY NOT NULL ,
					topic_tag_name varchar(255) NOT NULL DEFAULT '',
					topic_tag_colour INTEGER UNSIGNED NOT NULL DEFAULT '0',
					topic_tag_desc varchar(255) NOT NULL DEFAULT '',
					forum_id INTEGER UNSIGNED NOT NULL DEFAULT '0',
					topic_tag_global INTEGER UNSIGNED NOT NULL DEFAULT '0'
				);
				
				COMMIT;";
		
				$db->sql_query($sql);
			break;
		}
		
		// Setup $auth_admin class so we can add permission options
		include($phpbb_root_path . 'includes/acp/auth.' . $phpEx);
		$auth_admin = new auth_admin();
		
		// Add permission for manage cvsdb
		$auth_admin->acl_add_option(array(
			'local'		=> array('f_set_topic_tag', 'f_edit_topic_tag', 'm_edit_topic_tags'),
			'global'	=> array()
		));
		
		set_config('topic_tags_version', $topic_tags_version);
		
		trigger_error(sprintf($user->lang['TOPIC_TAGS_INSTALLED'], $topic_tags_version));
	}

	function uninstall()
	{
		global $db, $user;
		
		$db->sql_transaction('begin');
		
		$sql = 'DELETE topic_tags_version FROM ' . CONFIG_TABLE . ' WHERE config_name = \'topic_tags_version\'';
		$db->sql_query($sql);
		
		$sql = 'ALTER TABLE ' . TOPICS_TABLE . ' DROP COLUMN topic_tag_id';
		$db->sql_query($sql);
		
		$sql = 'ALTER TABLE ' . FORUMS_TABLE . ' DROP COLUMN forum_topic_tags';
		$db->sql_query($sql);
		
		$sql = 'DROP TABLE ' . TOPIC_TAGS_TABLE;
		$db->sql_query($sql);
		
		$db->sql_transaction('commit');
		
		trigger_error($user->lang['TOPIC_TAGS_UNINSTALLED']);
	}
	
	function update($topic_tags_version)
	{
		global $user;
		
		set_config('topic_tags_version', $topic_tags_version);
	
		trigger_error($user->lang['TOPIC_TAGS_UPDATED'], $topic_tags_version);
	}
}

?>